@isTest
private class AASQuoteControllerTest {

	static testMethod void controllerTestProductING() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getShowIYPBiddedPayPerCallWorksheet());
	
			if (AddProduct(o, 'ING')){			
				controller = new AASQuoteController(sc);
				System.assertEquals(true, controller.getShowIYPBiddedPayPerCallWorksheet());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductYPCALL() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getShowIYPFixedPayPerCallWorksheet());
	
			if (AddProduct(o, 'YPCALL')){			
				controller = new AASQuoteController(sc);
				System.assertEquals(true, controller.getShowIYPFixedPayPerCallWorksheet());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductYPBAN() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getShowIYPBannerWorksheet());
	
			if (AddProduct(o, 'YPBAN')){			
				controller = new AASQuoteController(sc);
				System.assertEquals(false, controller.getShowIYPBannerWorksheet());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductYPCLK() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getShowIYPPayPerClick());
	
			if (AddProduct(o, 'YPCLK')){			
				controller = new AASQuoteController(sc);
				System.assertEquals(true, controller.getShowIYPPayPerClick());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductYPSUB() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getShowIYPSubscription());
	
			if (AddProduct(o, 'YPSUB')){			
				controller = new AASQuoteController(sc);
				System.assertEquals(true, controller.getShowIYPSubscription());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductJTBAN() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getShowATTJumptabBanner());
	
			if (AddProduct(o, 'JTBAN')){			
				controller = new AASQuoteController(sc);
				System.assertEquals(true, controller.getShowATTJumptabBanner());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductJTSWP() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getshowATTJumptabSweepstakes());
	
			if (AddProduct(o, 'JTSWP')){			
				controller = new AASQuoteController(sc);
				System.assertEquals(true, controller.getshowATTJumptabSweepstakes());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductMOBAPP() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getshowMobileApplications());
	
			if (AddProduct(o, 'MOBAPP')){			
				controller = new AASQuoteController(sc);
				System.assertEquals(true, controller.getshowMobileApplications());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductPLSBAN() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getshowPlusmoMobileBanner());
	
			if (AddProduct(o, 'PLSBAN')){			
				controller = new AASQuoteController(sc);
				System.assertEquals(true, controller.getshowPlusmoMobileBanner());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductPPPC() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getShowPrintPayPerCall());
	
			if (AddProduct(o, 'PPPC')){			
				controller = new AASQuoteController(sc);
				System.assertEquals(true, controller.getShowPrintPayPerCall());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductPSUB() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getshowPrintSubscription());
	
			if (AddProduct(o, 'PSUB')){			
				controller = new AASQuoteController(sc);
				System.assertEquals(true, controller.getshowPrintSubscription());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductRNTK() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getshowRentrak());
	
			if (AddProduct(o, 'RNTK')){			
				controller = new AASQuoteController(sc);
				// no worksheet for RNTK after update, AD-24019
				System.assertEquals(false, controller.getshowRentrak());
				//System.assertEquals(true, controller.getshowRentrak());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductSCHEX() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getshowSearchExtension());
	
			if (AddProduct(o, 'SCHEX')){			
				controller = new AASQuoteController(sc);
				System.assertEquals(true, controller.getshowSearchExtension());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductUVRS() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getshowUverse());
	
			if (AddProduct(o, 'UVRS')){			
				controller = new AASQuoteController(sc);
				// no worksheet for UVRS after update, AD-24019
				System.assertEquals(false, controller.getshowUverse());
				//System.assertEquals(true, controller.getshowUverse());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductWIFI() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getshowWifi());
	
			if (AddProduct(o, 'WIFI')){			
				controller = new AASQuoteController(sc);
				System.assertEquals(true, controller.getshowWifi());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductYAHOO() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getshowYahoo());
	
			if (AddProduct(o, 'YAHOO')){			
				controller = new AASQuoteController(sc);
				System.assertEquals(true, controller.getshowYahoo());
			}
			
			Test.stopTest();	    	
	}

	static testMethod void controllerTestProductYPMBAN() {	    	
			Opportunity o = CreateOpportunity();	    
		    Quote q = CreateQuote(o);
		    
			//Use the PageReference Apex class to instantiate a page
			PageReference pageRef = Page.AASQuoteWorkSheetPage;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('ID', o.ID);
		        
		    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
	        
			Test.startTest();
		    AASQuoteController controller = new AASQuoteController(sc);
			System.assertEquals(false, controller.getshowYPMobileBanner());
	
			if (AddProduct(o, 'YPMBAN')){			
				controller = new AASQuoteController(sc);
				System.assertEquals(true, controller.getshowYPMobileBanner());
			}
			
			Test.stopTest();	    	
	}
	
	static testMethod void miscTest()
	{	
		Opportunity o = CreateOpportunity();	    
	    Quote q = CreateQuote(o);
	    
		//Use the PageReference Apex class to instantiate a page
		PageReference pageRef = Page.AASQuoteWorkSheetPage;
		Test.setCurrentPage(pageRef);
		ApexPages.currentPage().getParameters().put('ID', o.ID);
		
		ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
		AASQuoteController controller = new AASQuoteController(sc);
		
		/* default is view */
		system.assert(controller.getInViewMode() == true);
		
		/* change to edit mode */
		controller.editWorksheet();
		system.assert(controller.inEditMode == true);
		
		/* test cancel */
		controller.CancelSaveWorksheet();
		system.assert(controller.inEditMode == false);
		
		/* test normal save */
		controller.SaveWorksheet();
	}
	
	@isTest
	static void testMobileWorsheet()
	{	    
	    Opportunity o = CreateOpportunity();	    
	    Quote q = CreateQuote(o);
	    
	    for(String mpc : AASQuoteController.MOBILE_WS_PC)
	    {
	    	if(AddProduct(o, mpc))
	    	   break;
	    }
	    
	    PageReference pageRef = Page.AASQuoteWorkSheetPage;
		Test.setCurrentPage(pageRef);
		ApexPages.currentPage().getParameters().put('ID', o.ID);
		
		ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
		AASQuoteController controller = new AASQuoteController(sc);
		
		 /* test add mobile attachment */
	    System.assertEquals(true, controller.getShowMobileAudienceNetwork()); // init section
	    controller.Mobile_Geo_File.Name = 'Test.txt';
	    controller.Mobile_Geo_File.Body = Blob.valueOf('afefaew');
	   
	    controller.SaveWorksheet();
	    system.assert(controller.Mobile_Geo_File.Id != null);
	}
	
	@isTest
	static void testOnlineWorsheet()
	{	    
	    Opportunity o = CreateOpportunity();	    
	    Quote q = CreateQuote(o);
	    
	    for(String mpc : AASQuoteController.ONLINE_WS_PC)
	    {
	    	if(AddProduct(o, mpc))
	    	   break;
	    }
	    
	    PageReference pageRef = Page.AASQuoteWorkSheetPage;
		Test.setCurrentPage(pageRef);
		ApexPages.currentPage().getParameters().put('ID', o.ID);
		
		ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(q);
		AASQuoteController controller = new AASQuoteController(sc);
		
		/* test add online attachment */
	    System.assertEquals(true, controller.getShowOnlineAudienceNetwork()); // init section
	    controller.Online_Geo_File.Name = 'Test.txt';
	    controller.Online_Geo_File.Body = Blob.valueOf('afefaew');
	   
	    controller.SaveWorksheet();
	    system.assert(controller.Online_Geo_File.Id != null);
	}

	private static Opportunity CreateOpportunity(){
			Opportunity o = new Opportunity();
			o.RecordTypeId = RecordTypeHelper.GetRecordTypeId('Opportunity', 'Advanced Ad Solutions');
			o.Name = 'Unit Test';
			o.StageName = 'New';
			o.CloseDate = Date.today();
			insert o;
			
			return o;
	}
	
	private static Quote CreateQuote(Opportunity o){
		Quote q = new Quote();
	    q.OpportunityID = o.id;
	    q.Name = 'Unit Test';
	    insert q;
	
		return q;
	}
	
	private static boolean AddProduct(Opportunity o, string productCode){
		OpportunityLineItem oli = new OpportunityLineItem();
		oli.OpportunityId = o.ID;
		oli.Quantity = 1;
		oli.TotalPrice = 1;		
		//oli.productId = [Select id from product2 where ProductCode  = :productCode limit 1].id;
		List<PricebookEntry> pbe = [Select id from PricebookEntry  where ProductCode  = :productCode and IsActive = true limit 1];
		if(pbe.size() == 1){
			oli.PricebookEntryId = pbe[0].id; 		
			insert oli;
			
			return true;
		}
		return false;
	}
}